<?php

namespace app\admin\controller\sta\batch;

use app\admin\model\Factory;
use app\admin\model\Pinxi;
use app\admin\model\Store;
use app\admin\model\Storea;
use app\admin\model\Storesub;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 生物资产收发存汇总管理
 *
 * @icon fa fa-circle-o
 */
class Bios extends Backend
{

    /**
     * Bios模型对象
     * @var \app\admin\model\sta\batch\Bios
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sta\batch\Bios;
        $this->view->assign("statusList", $this->model->getStatusList());

        // demo  日期数组,用于快捷筛选条件
        $firstOfBios = Db::name("sta_batch_bios")->order("createtime ASC")->select();
        $start = $firstOfBios[0] ? $firstOfBios[0]['createtime'] : time();

        $startMonth = date("Y-m", $start);
        $nextMonth = date('Y-m', strtotime('+1 month'));
        $monthList = createMonlist($startMonth, $nextMonth );

        $this->view->assign("monthList", $monthList);
        $this->assignconfig("monthList", $monthList);

        $this->assignconfig("factoryList", Factory::getList() );
        $this->assignconfig("storesubList", Storesub::getList() );
        $this->assignconfig("storeaList", Storea::getList() );
        $this->assignconfig("pinxiList", Pinxi::getList() );
    }

    // 按钮:  汇总x月的数据, 并保存到当月汇总数据
    // 引用:  如果系统时间, 大于汇总表 sta_batch_bios 的时间, 则 1创建月初数据 2汇总上月数据,并标记已完成
    public function summonth__test(){
        $cal_month = "2022-05";
        $this->index_cal($cal_month);
    }

    /**
     * 统计
     *
     * @return string
     * @throws \think\Exception
     */
    public function summonth()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);

        // todo 增加强制更新, 或者允许删除后, 重新生成

        // todo 如果存在上月数据, 或更早的数据, 结算之前的数据, 创建这个月的期初数据


        // 统计月份
        $exist = Db::name("sta_batch_bios")
            ->where("createtime", strtotime($params['createtime']) )
            ->where("status", "locked" )
            ->select();
        if( count($exist) ){
            $result = false;
        }else{
            //  是否需要校验
            $cal_month = $params['createtime'];
            //  计算
            $result = $this->index_cal($cal_month);
        }

        if ($result === false) {
            $this->error(__('汇总数据已存在,无法覆盖已存在的数据'));
        }
        if ($result['lines'] == 0) {
            $this->error(__('未获取到有效数据! '));
        }
        $this->success(__('汇总数据生成完毕.'). " 更新行数: ". json_encode($result['lines'])  );
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $list = $this->getXXX($list);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    // 计算x月的汇总数据    格式 "2022-05"
    public function index_cal($month)
    {
        //   条件是:   批次有出栏??

        //  TODO:  功能完成后, 需要改为: 查询完成上月的数据后,记录存到表 xxx, 并锁定数据不可修改
        //  TODO:  查询条件:  月份,当月可修改;  (可能还会按厂区拆分)
        //   @以全部库求汇总, 再处理数组  #$storem1 = new \app\admin\model\Storem1 ;
        //  计算月份      *
        //   todo   需要改成可选月份
        //   查询本月数据是随动的,单条记录不可修改.  查询上月数据是已存到表 sta_material 的, 查询当月是即时的
        //   todo    $newdate  需要做成用户选择

        // 月底
        #$month_end = date("Y-m-d", strtotime("+1 month", strtotime($month) )  );
        $month_end = strtotime("+1 month", strtotime($month) );
        $last_month = strtotime("-1 month", strtotime($month) );
        $month_start = strtotime($month);
        // 汇总月份
        $this->view->assign("mark", date("ym", strtotime($month) )."月" );

        $factoryList = Factory::where("status",1)->field("id, name")->select();

        # 汇总月的实时数据:
        $storepro_sum = [];
        foreach($factoryList as &$factory) {
            $list = Db::name("storepro")->alias("sp")
                ->field("id as sp_id, factory_id, storea_id, field2, field2 as pinxi_id, sum(cal_num) as stock_num ")
                //->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("createtime", "between",  [$month_start, $month_end] )
                ->where("deletetime is null")
                ->where("factory_id", $factory['id'])
                ->group("sp.storea_id, sp.field2")
                ->select();
            //sqq();
            $storepro_sum = array_merge($storepro_sum, $list);
        }
//print_r($storepro_sum);die;
        // TODO  一定获取上次或上月的数据, 如果无, 则为0, 如果没有批次, 则不显示

        $sta_lastm = [];
        foreach($factoryList as &$val){
            $fc = Db::name("sta_batch_bios")->field("*")
                ->where("factory_id", $val['id'])
                //->where("storea_id", $val['storea_id'])
                //->where("pinxi_id", $val['field2'])
                //->where("DATE_FORMAT(createtime,'%Y-%m-%d')='{$lastmonth}' ")
                ->where("createtime", "between",  [$last_month, $month_start] )
                ->select();
            //sqq();
            $sta_lastm = array_merge($sta_lastm, $fc);
        }
//print_r($sta_lastm);die;

        # 上月数据直接结转, 复制基础字段
        $this_month_tmp = [] ;
        foreach($sta_lastm as $cur ){
            $new = [
                "createtime" => $month_start,
                "factory_id" => $cur['factory_id'],
                "storesub_id" => $cur['storesub_id'],
                "storea_id" => $cur['storea_id'],
                "gene_id" => $cur['gene_id'],
                "pinxi_id" => $cur['pinxi_id'],
                "storem1_id" => $cur['storem1_id'],
                "a1" => $cur['g1'],     // 上月期末数据
                "a2" => $cur['g2'],
                "a3" => $cur['g3']
            ];
            array_push($this_month_tmp, $new);
        }
//print_r($this_month_tmp);die;
        # 合并期初数据, 新增数据 : 只要行字段
        $this_month_all = $this_month_tmp;
        foreach($storepro_sum as $k => &$val){
            // 获取上月结算数据,  可能存在无的情况
            $last_data = Db::name("sta_batch_bios")
                ->where([
                    'factory_id'=> $val['factory_id'],
                    'storea_id'=> $val['storea_id'],
                    'pinxi_id'=> $val['pinxi_id'],
                    'createtime'=> $last_month,
                ])->find();

            $val['a1'] = $last_data ? $last_data['g1'] : 0 ;                 //  a1  '月初存栏',
            $val['a2'] = $last_data ? $last_data['g2'] : 0 ;                 //  a2  '单价',
            $val['a3'] = $last_data ? $last_data['g3'] : 0 ;                 //  a3  '金额',
            $val['createtime'] = $month_start ;
            #$val['last_model'] = $last_data ;

            if(!$last_data){
                array_push($this_month_all, $val);
            }
        }
        # 合并后的行数据: $this_month_tmp: 只有行, 期初数量,
//        [1] => Array
//            (
//                [createtime] => 1654012800
//                    [factory_id] => 1
//                    [storesub_id] =>
//                    [storea_id] => 2
//                    [gene_id] =>
//                    [pinxi_id] => 6
//                    [storem1_id] =>
//                    [a1] => 9400
//                    [a2] =>
//                    [a3] =>
//                )
//print_r($this_month_tmp);die;
//print_r($set);die;

//print_r($this_month_all); die;

        unset($set);
        $set = $this_month_all;
        foreach($set as $k => $val){
            #月初数
//            $last = Db::name("sta_batch_bios")->field("*")
//                ->where("factory_id", $val['factory_id'])
//                ->where("storea_id", $val['storea_id'])
//                ->where("pinxi_id", $val['field2'])
//                //->where("DATE_FORMAT(createtime,'%Y-%m-%d')='{$lastmonth}' ")
//                ->where("createtime", "between",  [$last_month, $month] )
//                ->find();

            if(1) {
                #转为生长
                $to_grow = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("from_factory_id", $val['factory_id'])
                    //->where("from_storea_id", 0 )
                    ->where("to_factory_id", $val['factory_id'])
                    ->where("to_storea_id", 3)
                    ->where("field2", $val['field2'])
                    ->where("cal_num", "lt", 0)
                    ->select();

                #种兔自繁
                $to_zhongtu_self = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("from_factory_id", $val['factory_id'])
                    ->where("from_storea_id", 5)
                    ->where("to_factory_id", $val['factory_id'])
                    ->where("to_storea_id", 1)
                    ->where("field2", $val['field2'])
                    ->where("cal_num", "lt", 0)
                    ->select();

                #转出
                $to_out = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("factory_id", $val['factory_id'])
                    ->where("storea_id", $val['storea_id'])
                    ->where("field2", $val['field2'])
                    ->where("cal_num", "lt", 0)
                    ->select();
                #转入
                $to_in = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("factory_id", $val['factory_id'])
                    ->where("storea_id", $val['storea_id'])
                    ->where("field2", $val['field2'])
                    ->where("cal_num", "gt", 0)
                    ->select();

                #淘汰数
                $to_minus = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("factory_id", $val['factory_id'])
                    ->where("storea_id", $val['storea_id'])
                    ->where("field2", $val['field2'])
                    ->where("cal", "淘汰出库")
                    ->select();
                #死亡
                $to_die = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("factory_id", $val['factory_id'])
                    ->where("storea_id", $val['storea_id'])
                    ->where("field2", $val['field2'])
                    ->where("cal", "死亡出库")
                    ->select();
                # 销售出
                $to_sale = Db::name("storepro")->alias("sp")
                    ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                    ->where("createtime", "between", [$month_start, $month_end])
                    ->where("deletetime is null")
                    ->where("factory_id", $val['factory_id'])
                    ->where("storea_id", $val['storea_id'])
                    ->where("field2", $val['field2'])
                    ->where("cal", "销售出库")
                    ->select();
            }
//            echo Db::getLastSql();die;
            # 即时数据
//            $cur = Db::name("storepro")
////                ->where("storea_id", $val['storea_id'])
////                ->where("cal", "销售")
////                ->field("*, sum(num) as sum_num ")
////                ->find();
//echo Db::getLastSql();die;

//            $set[$k]['a1'] = $last ? $last['g1'] : 0 ;                 //  a1  '月初存栏',
//            $set[$k]['a2'] = $last ? $last['g2'] : 0 ;                 //  a2  '单价',
//            $set[$k]['a3'] = $last ? $last['g3'] : 0 ;                 //  a3  '金额',

//            // 获取上月结算数据,  可能存在无的情况
//            $last_data = Db::name("sta_batch_bios")
//                ->where([
//                    'factory_id'=> $val['factory_id'],
//                    'storea_id'=> $val['storea_id'],
//                    'pinxi_id'=> $val['pinxi_id'],
//                    'createtime'=> $last_month,
//                ])->find();
//
//            $set[$k]['a1'] = $last_data ? $last_data['g1'] : 0 ;                 //  a1  '月初存栏',
//            $set[$k]['a2'] = $last_data ? $last_data['g2'] : 0 ;                 //  a2  '单价',
//            $set[$k]['a3'] = $last_data ? $last_data['g3'] : 0 ;                 //  a3  '金额',

            $set[$k]['b1'] = $to_grow ? $to_grow[0]['sum_num'] : 0;                 //  b1  '转为生长',
            $set[$k]['b2'] = $to_zhongtu_self ? $to_zhongtu_self[0]['sum_num'] : 0;                 //  b2  '种兔自繁',
            $set[$k]['b3'] = $to_in ? $to_in[0]['sum_num'] : 0;                     //  b3  '转入数 ',
            $set[$k]['b4'] = 0;                 //  b4  '单价',
            $set[$k]['b5'] = 0;                 //  b5  '金额',

            $set[$k]['c1'] = $to_out ? $to_out[0]['sum_num'] : 0;                 //  c1  '转出数',
            $set[$k]['c2'] = 0;                 //  c2  '单价',
            $set[$k]['c3'] = 0;                 //  c3  '金额',

            $set[$k]['d1'] = $to_minus ? $to_minus[0]['sum_num'] : 0;                 //  d1  '淘汰数',
            $set[$k]['d2'] = 0;                 //  d2  '单价',
            $set[$k]['d3'] = 0;                 //  d3  '金额',

            $set[$k]['e1'] = $to_die ? $to_die[0]['sum_num'] : 0;                 //  e1  '死亡数',
            $set[$k]['e2'] = "";                 //  e2  '单价',
            $set[$k]['e3'] = "";                 //  e3  '金额',

            $set[$k]['f1'] = $to_sale ? $to_sale[0]['sum_num'] : 0 ;                //  f1  '销售数',
            $set[$k]['f2'] = 1;                                     //  f2  '单价',
            $set[$k]['f3'] = $set[$k]['f1'] * $set[$k]['f2'];                 //  f3  '金额',

            //  g1  '月末存栏',   //$val['stock_num'] x暂不计明细表的求和数量
            $set[$k]['g1'] = intval($set[$k]['a1']) + intval($set[$k]['b3']) - intval($set[$k]['c1']) ;
            #$set[$k]['g1'] = intval($set[$k]['a1']) + intval($set[$k]['b3']) - intval($set[$k]['c1']) - intval($set[$k]['d1']) - intval($set[$k]['e1']) - intval($set[$k]['f1']) ;
            #$set[$k]['g1'] = intval($set[$k]['a1']) + intval($val['stock_num']) ;
            $set[$k]['g2'] = "";                 //  g2  '单价',
            $set[$k]['g3'] = "";                 //  g3  '金额',

            $set[$k]['createtime'] = strtotime($month) ;
            $set[$k]['status'] = $month_end < time() ? "locked" : "normal" ;
        }
//print_r($set);die;
        ## 保存到汇总表
        if( empty($set) ){
            return 0;
        }
//exit;

        // todo  如果显示了当月数据, 重新生成需要删除当月的临时数据.
        $stabiosModel = new \app\admin\model\sta\batch\Bios ;
        $res_all = $stabiosModel->allowField(true)->saveAll($set) ;

        return ['data'=> $set, 'lines'=> count($res_all) ];
    }

    private function get_last_sta($va, $list){
//        foreach($list as )
    }


    public function index2__bak()
    {
        //   全部批次为行,  条件是:   批次有出栏

//  TODO:  功能完成后, 需要改为: 查询完成上月的数据后,记录存到表 xxx, 并锁定数据不可修改
//  TODO:  查询条件:  月份,当月可修改;  (可能还会按厂区拆分)

        //   @以全部库求汇总, 再处理数组,
        $storem1 = new \app\admin\model\Storem1 ;
        //  计算月份      *
        //   todo   需要改成可选月份
        //   查询本月数据是随动的,单条记录不可修改.  查询上月数据是已存到表 sta_material 的, 查询当月是即时的
        //   todo    $newdate  需要做成用户选择
//        $newdate = date("Y-02-01");          // 月初       todo
//        $month = date("Y-02");              // 月初       todo

        $newdate = date("Y-03-01");          // 月初       todo
        $month = date("Y-03");              // 月初       todo

        $newdate__test = date("Y-02-01");          // 月初       todo



        $newdate_31 = date("Y-m-d", strtotime("+1 month", strtotime($newdate) )  );    // 月底
        // 汇总月份
        $this->view->assign("mark", date("ym", strtotime($newdate) )."月" );

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        // 关联表查询
//        $pinxiList = Pinxi::getSelectList();
//        $depotList = Depot::getList();
//        $factoryList = Factory::getList();
//

//print_r($where);die;
        #  数据行  1
//        $list = $storem1
//            ->where($where)
//            ->field("*")
//            ->order("id ASC")
//            ->paginate($limit);
        #  2
//        $list = $storem1->alias("m1")
//            ->where($where)
//            ->join("storepro sp", "sp.storem1_id = m1.id", "right" )
//            ->join("batch b", "b.id = sp.batch_id", "right" )
//            ->field("m1.*, sp.id as sp_id, b.pinxi_id ")
//            ->order("m1.factory_id ASC")
//            ->paginate($limit);
        #  3
//        $list = Db::name("storepro")->alias("sp")
//            //->join("storepro sp", "sp.storem1_id = m1.id", "right" )
//            ->join("batch b", "b.id = sp.batch_id", "left" )
//            ->join("pinxi p", "p.id = b.pinxi_id", "left" )
//            ->field("sp.* , p.daici as gene_id, b.pinxi_id ")
//            ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
//            //->order("sp.factory_id ASC")
//            ->paginate($limit);
        # 4
//        $factory = Factory::where("status",1)->select();
//print_r( Collection($factory)->toArray() );  die;
        $factoryList = Factory::where("status",1)->field("id, name")->select();

        $set = [];
        foreach($factoryList as &$factory) {
            $list = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(cal_num) as stock_num ")
//                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
//                ->where("deletetime is null")
                ->where("factory_id", $factory['id'])
                ->group("sp.field2")
                ->select();
            $set = array_merge($set, $list);
        }

//print_r(  $factory->items()  );  die;
//print_r( Collection($list->items())->toArray() );  die;
//print_r( Collection($set)->toArray() );  die;

        // TODO:  如果计算合计,  那么要按当前筛选条件, 进行`不分页` 计算
//print_r($list);die;
        // TODO:  采集对应数据

        foreach($set as $k => $val){
            #月初数
            $last = Db::name("sta_batch_bios")->field("*")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("pinxi_id", $val['field2'])
                ->where("DATE_FORMAT(createtime,'%Y-%m-%d')='{$newdate__test}' ")
                ->find();
            #转为生长
            $to_grow = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("from_factory_id", $val['factory_id'])
                //->where("from_storea_id", 0 )
                ->where("to_factory_id", $val['factory_id'] )
                ->where("to_storea_id", 3 )
                ->where("field2", $val['field2'])
                ->where("cal_num", "lt", 0)
                ->select();

            #种兔自繁
            $to_zhongtu_self = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("from_factory_id", $val['factory_id'])
                ->where("from_storea_id", 5 )
                ->where("to_factory_id", $val['factory_id'] )
                ->where("to_storea_id", 1 )
                ->where("field2", $val['field2'])
                ->where("cal_num", "lt", 0)
                ->select();

            #转出
            $to_out = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("field2", $val['field2'])
                ->where("cal_num", "lt", 0)
                ->select();
            #转入
            $to_in = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("field2", $val['field2'])
                ->where("cal_num", "gt", 0)
                ->select();

            #淘汰数
            $to_minus = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("field2", $val['field2'])
                ->where("cal", "淘汰出库")
                ->select();
            #死亡
            $to_die = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("field2", $val['field2'])
                ->where("cal", "死亡出库")
                ->select();
            # 销售出
            $to_sale = Db::name("storepro")->alias("sp")
                ->field("id, factory_id, storea_id, field2, sum(num) as sum_num ")
                ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
                ->where("deletetime is null")
                ->where("factory_id", $val['factory_id'])
                ->where("storea_id", $val['storea_id'])
                ->where("field2", $val['field2'])
                ->where("cal", "销售出库")
                ->select();

//            echo Db::getLastSql();die;
            # 即时数据
//            $cur = Db::name("storepro")
////                ->where("storea_id", $val['storea_id'])
////                ->where("cal", "销售")
////                ->field("*, sum(num) as sum_num ")
////                ->find();
//echo Db::getLastSql();die;

            $set[$k]['a1'] = $last ? $last['g1'] : 0 ;                 //  a1  '月初存栏',
            $set[$k]['a2'] = $last ? $last['g2'] : 0 ;                 //  a2  '单价',
            $set[$k]['a3'] = $last ? $last['g3'] : 0 ;                 //  a3  '金额',

            $set[$k]['b1'] = $to_grow ? $to_grow[0]['sum_num'] : 0;                 //  b1  '转为生长',
            $set[$k]['b2'] = $to_zhongtu_self ? $to_zhongtu_self[0]['sum_num'] : 0;                 //  b2  '种兔自繁',
            $set[$k]['b3'] = $to_in ? $to_in[0]['sum_num'] : 0;                     //  b3  '转入数 ',
            $set[$k]['b4'] = 0;                 //  b4  '单价',
            $set[$k]['b5'] = 0;                 //  b5  '金额',

            $set[$k]['c1'] = $to_out ? $to_out[0]['sum_num'] : 0;                 //  c1  '转出数',
            $set[$k]['c2'] = 0;                 //  c2  '单价',
            $set[$k]['c3'] = 0;                 //  c3  '金额',

            $set[$k]['d1'] = $to_minus ? $to_minus[0]['sum_num'] : 0;                 //  d1  '淘汰数',
            $set[$k]['d2'] = 0;                 //  d2  '单价',
            $set[$k]['d3'] = 0;                 //  d3  '金额',

            $set[$k]['e1'] = $to_die ? $to_die[0]['sum_num'] : 0;                 //  e1  '死亡数',
            $set[$k]['e2'] = "";                 //  e2  '单价',
            $set[$k]['e3'] = "";                 //  e3  '金额',

            $set[$k]['f1'] = $to_sale ? $to_sale[0]['sum_num'] : 0 ;                //  f1  '销售数',
            $set[$k]['f2'] = 1;                                     //  f2  '单价',
            $set[$k]['f3'] = $set[$k]['f1'] * $set[$k]['f2'];                 //  f3  '金额',

            $set[$k]['g1'] = "";                 //  g1  '月末存栏',
            $set[$k]['g2'] = "";                 //  g2  '单价',
            $set[$k]['g3'] = "";                 //  g3  '金额',


        }

        $set = $this->getXXX($set);
        $storeaList = Storea::getList();
        $pinxiList = Pinxi::getList();
        foreach($set as &$val){
            $val['storea_id'] = isset($storeaList[$val['storea_id']]) ? $storeaList[$val['storea_id']] : "";
            $val['pinxi_id'] = isset($pinxiList[$val['field2']]) ? $pinxiList[$val['field2']] : "" ;
        }
//        print_r($list);die;
        $result = ['total' => 0, 'rows' => $set];
        return json($result);
    }



}
